/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.listeners.automation
{
	import flash.system.Capabilities;
	
	import funit.core.ITestListener;
	import funit.core.TestName;
	import funit.core.TestOutput;
	import funit.core.TestResult;
	import funit.core.VersionInfo;
	import funit.framework.MessageString;
	import funit.framework.MessageUtils;
	import funit.writers.DebugWriter;
	import funit.writers.IWriter;
	
	import mx.collections.ArrayCollection;
	import mx.utils.StringUtil;
	
	public class DebugTestListener implements ITestListener
	{
		private var level:int;
		private var testRunCount:int;
		private var testIgnoreCount:int;
		private var failureCount:int;
		
		private var messages:ArrayCollection;
		private var unhandledExceptions:ArrayCollection;
		
		private var currentTestName:String;
		
		private var _writer:IWriter;
		
		
		/**
		 * @param writer
		 */
		public function DebugTestListener( writer:IWriter = null )
		{
			this.writer = writer;
			
			level = 0;
		}
		
		
		/**
		 * 
		 */
		private function writeDivider() : void
		{
			_writer.writeLine("------------------------------------------------------------------");
		}
		
		/**
		 * @param name
		 * @param testCount
		 */
		public function runStarted( name:String, testCount:int ) : void
		{
			writeDivider();
			
			_writer.writeLine("  {0} - {1}", VersionInfo.name, VersionInfo.description);
			_writer.writeLine("  Framework Version: {0} (Build {1})", VersionInfo.version, VersionInfo.build);
			
			_writer.writeLine();
			
			_writer.writeLine("  {0} ({1})", Capabilities.manufacturer, Capabilities.playerType);
			_writer.writeLine("  Player Version: {0}", Capabilities.version);
			
			writeDivider();
			
			_writer.writeLine();
		}
		
		/**
		 * @param testResult
		 */
		public function runFinished( testResult:TestResult ) : void
		{
			
		}
		
		/**
		 * @param error
		 */
		public function runFinishedError( error:Error ) : void
		{
			
		}
		
		/**
		 * @param testName
		 */
		public function testStarted( testName:TestName ) : void
		{
			currentTestName = testName.fullName;
			
			_writer.writeLine("***** {0}", currentTestName );
		}
		
		/**
		 * @param testResult
		 */
		public function testFinished( testResult:TestResult ) : void
		{
			if (testResult.executed)
			{
				testRunCount++;
				
				if (testResult.isFailure)
				{
					failureCount++;
					
					messages.addItem( StringUtil.substitute( "{0}) {1} :", failureCount, currentTestName ) );
					messages.addItem( MessageUtils.trimCharacter(testResult.message, MessageString.NewLine) );
					
					var stackTrace:String = testResult.stackTrace;
					
					if (stackTrace)
					{
						messages.addItem(stackTrace);
					}
				}
			}
			else
			{
				testIgnoreCount++;
			}
			
			currentTestName = "";
		}
		
		/**
		 * @param suiteName
		 */
		public function suiteStarted( suiteName:TestName ) : void
		{
			if ( level++ == 0 )
			{
				messages = new ArrayCollection();
				unhandledExceptions = new ArrayCollection();
				testRunCount = 0;
				testIgnoreCount = 0;
				failureCount = 0;
				_writer.writeLine( "################################ UNIT TESTS ################################" );
				_writer.writeLine(
					(suiteName) ? "Running tests in '" + suiteName.fullName + "'..." : "Running tests ..."
				);
			}
		}
		
		/**
		 * @param suiteResult
		 */
		public function suiteFinished( suiteResult:TestResult ) : void
		{
			if ( --level == 0 )
			{
				_writer.writeLine( "############################################################################" );
				
				if (messages.length == 0)
				{
					_writer.writeLine( "##############                 S U C C E S S               #################" );
				}
				else
				{
					_writer.writeLine( "##############                F A I L U R E S              #################" );
					
					for each ( var message:String in messages )
					{
						_writer.writeLine(message);
					}
				}
				
				_writer.writeLine( "############################################################################" );
				_writer.writeLine( "Executed tests       : " + testRunCount );
				_writer.writeLine( "Ignored tests        : " + testIgnoreCount );
				_writer.writeLine( "Failed tests         : " + failureCount );
				_writer.writeLine( "Unhandled exceptions : " + unhandledExceptions.length);
				_writer.writeLine( "Total time           : " + (suiteResult.time / 1000) + " seconds" );
				_writer.writeLine( "############################################################################");
			}
		}
		
		/**
		 * @param error
		 */
		public function unhandledError( error:Error ) : void
		{
			unhandledExceptions.addItem(currentTestName + " : " + error);
			
			var msg:String = StringUtil.substitute( "##### Unhandled Exception while running {0}", currentTestName );
			
			_writer.writeLine(msg);
			_writer.writeLine(error.toString());
		}
		
		/**
		 * @param testOutput
		 */
		public function testOutput( testOutput:TestOutput ) : void
		{
			
		}
		
		
		/**
		 * 
		 */
		public function get writer() : IWriter
		{
			return _writer;
		}
		
		public function set writer( value:IWriter ) : void
		{
			if (value != null)
			{
				_writer = value;
			}
			else
			{
				_writer = new DebugWriter();
			}
		}
		
	}
	
}
